package cn.xtxfan.trigger.http;

import cn.xtxfan.domain.user.model.dto.CheckUserDTO;
import cn.xtxfan.domain.user.model.entity.CheckUserEntity;
import cn.xtxfan.domain.user.service.ICheckUserService;
import cn.xtxfan.types.enums.ResponseCode;
import cn.xtxfan.types.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;

/**
 * @className: UserController
 * @author: XTX
 * @date: 2025/7/19 15:58
 * @Version: 1.0
 * @description:
 */
@RestController
@RequestMapping("/check/user")
@Slf4j
@CrossOrigin("*")
public class UserController {

    @Resource
    private ICheckUserService checkUserService;

    @GetMapping("/login")
    public Response<CheckUserDTO> checkLogin(@NotEmpty @RequestParam("username") String username,
                                             @NotEmpty @RequestParam("password") String password) {
        log.info("接收到登录请求 username: {}, password: {}", username, password);
        try {
            CheckUserEntity checkUserEntity = new CheckUserEntity();
            checkUserEntity.setUsername(username);
            checkUserEntity.setPassword(password);
            CheckUserDTO checkUserDTO = checkUserService.queryCheckUser(checkUserEntity);
            if (checkUserDTO == null) {
                return Response.<CheckUserDTO>builder()
                        .code(ResponseCode.LOGIN_FAIL.getCode())
                        .info(ResponseCode.LOGIN_FAIL.getInfo())
                        .build();
            }
            return Response.<CheckUserDTO>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(checkUserDTO)
                    .build();

        } catch (Exception e) {
            log.error(e.toString());
            return Response.<CheckUserDTO>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

}

